package com.mr.security.web.commons.validate.smscode;

import com.mr.security.web.commons.utils.Constants;
import com.mr.security.web.commons.validate.AbstractValidateCodeProcessor;
import com.mr.security.web.commons.validate.entity.ValidateCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.context.request.ServletWebRequest;

/**
 * Class: SmsCodeProcessor
 * User (作者):MRui
 * TODO (描述)：sms短信验证码处理器
 * Date 2019-01-26 12:02
 */
@Component("smsIValidateCodeProcessor")
public class SmsCodeProcessor extends AbstractValidateCodeProcessor<ValidateCode> {


    /**
    * User (作者): MRui
    * TODO (描述)：短信发送接口例如阿里大于短信服务商实现（如需重写，直接实现此接口即可替换此接口默认处理方式）
    * Data：2019-01-26 12:05
    */
    @Autowired
    private SmsCodeSender smsCodeSender;
    /**
    * User (作者): MRui
    * TODO (描述)：短信验证码发送
    * Data：2019-01-26 12:02
    */
    @Override
    protected void send(ServletWebRequest request, ValidateCode validateCode) throws Exception {
        String paramName = Constants.DEFAULT_PARAMETER_NAME_MOBILE;
        //从request中根据定义的常量参数获取手机号
        String mobile = ServletRequestUtils.getRequiredStringParameter(request.getRequest(), paramName);
        //调用具体的短信发送接口
        smsCodeSender.send(mobile, validateCode.getCode());
    }
}
